package com.guldan.jaina.provider;

import java.util.List;

import com.guldan.core.constants.CommonConstant;
import com.guldan.jaina.dto.organization.OrganizationDTO;
import com.guldan.jaina.manager.*;
import com.guldan.jaina.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.guldan.core.dubbo.client.ResponseDTO;
import com.guldan.core.search.Pagination;
import com.guldan.jaina.dto.project.ProjectDTO;
import com.guldan.jaina.dto.project.ProjectSearchDTO;
import com.guldan.jaina.dto.projectattach.ProjectAttachDTO;
import com.guldan.jaina.dto.projectattach.ProjectAttachSearchDTO;
import com.guldan.jaina.dto.projectcategory.ProjectCategoryDTO;
import com.guldan.jaina.dto.projectcategory.ProjectCategorySearchDTO;
import com.guldan.jaina.dto.projectextensiondic.ProjectExtensionDicDTO;
import com.guldan.jaina.dto.projectextensiondic.ProjectExtensionDicSearchDTO;
import com.guldan.jaina.dto.projecttag.ProjectTagDTO;
import com.guldan.jaina.dto.projecttag.ProjectTagSearchDTO;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:54
 */
@Component
public class ProjectProviderImpl implements IProjectProvider {

    @Autowired
    private ProjectManager projectManager;

    @Autowired
    private ProjectTagManager projectTagManager;

    @Autowired
    private ProjectAttachManager projectAttachManager;

    @Autowired
    private OrganizationManager organizationManager;

    @Override
    public ResponseDTO<ProjectDTO> get(Integer areaId) {
        ResponseDTO<ProjectDTO> response = new ResponseDTO<>();
        response.setData(projectManager.get(areaId));
        return response;
    }

    @Override
    public ResponseDTO<ProjectDTO> save(ProjectDTO dto) {
        ResponseDTO<ProjectDTO> response = new ResponseDTO<>();

        try {
            response.setData(projectManager.save(dto));
        } catch (Exception ex) {
            response.setStatus(false);
            response.setException(ex);
        }
        return response;
    }

    @Override
    public ResponseDTO<Boolean> delete(Integer areaId) {
        ResponseDTO<Boolean> response = new ResponseDTO<>();
        response.setData(projectManager.delete(areaId));
        return response;
    }

    @Override
    public ResponseDTO<Pagination<ProjectDTO>> list(ProjectSearchDTO projectSearchDTO) {
        ResponseDTO<Pagination<ProjectDTO>> response = new ResponseDTO<>();

        Pagination<ProjectDTO> result = new Pagination<ProjectDTO>(projectSearchDTO.getCurrentPage(), projectSearchDTO.getPerPage());
        if (UserUtils.getUser().getUserRole().equals(CommonConstant.ROLE_Org)) {
            //如果是主管部门角色，则只查询主管部门负责的项目列表
            OrganizationDTO orgDto = organizationManager.getByOrgCode(UserUtils.getUser().getWorkOrg());
            projectSearchDTO.setOrgCode(orgDto.getBusinessDeptCode());

            result.setList(projectManager.listForOrg());
        }
        result = projectManager.list(projectSearchDTO);
        response.setData(result);
        return response;
    }

    @Autowired
    private ProjectCategoryManager projectCategoryManager;

    @Override
    public ResponseDTO<ProjectCategoryDTO> getProjectCategory(Integer areaId) {
        ResponseDTO<ProjectCategoryDTO> response = new ResponseDTO<>();
        response.setData(projectCategoryManager.get(areaId));
        return response;
    }

    @Override
    public ResponseDTO<ProjectCategoryDTO> saveProjectCategory(ProjectCategoryDTO dto) {
        ResponseDTO<ProjectCategoryDTO> response = new ResponseDTO<>();

        try {
            response.setData(projectCategoryManager.save(dto));
        } catch (Exception ex) {
            response.setStatus(false);
            response.setException(ex);
        }
        return response;
    }

    @Override
    public ResponseDTO<Boolean> deleteProjectCategory(Integer areaId) {
        ResponseDTO<Boolean> response = new ResponseDTO<>();
        response.setData(projectCategoryManager.delete(areaId));
        return response;
    }

    @Override
    public ResponseDTO<List<ProjectCategoryDTO>> allProjectCategory(ProjectCategorySearchDTO projectCategorySearchDTO) {
        ResponseDTO<List<ProjectCategoryDTO>> response = new ResponseDTO<>();
        response.setData(projectCategoryManager.all(projectCategorySearchDTO));
        return response;
    }

    @Autowired
    private ProjectExtensionDicManager projectExtensionDicManager;

    @Override
    public ResponseDTO<ProjectExtensionDicDTO> getProjectExtensionDic(Integer areaId) {
        ResponseDTO<ProjectExtensionDicDTO> response = new ResponseDTO<>();
        response.setData(projectExtensionDicManager.get(areaId));
        return response;
    }

    @Override
    public ResponseDTO<ProjectExtensionDicDTO> saveProjectExtensionDic(ProjectExtensionDicDTO dto) {
        ResponseDTO<ProjectExtensionDicDTO> response = new ResponseDTO<>();
        response.setData(projectExtensionDicManager.save(dto));
        return response;
    }

    @Override
    public ResponseDTO<Boolean> deleteProjectExtensionDic(Integer areaId) {
        ResponseDTO<Boolean> response = new ResponseDTO<>();
        response.setData(projectExtensionDicManager.delete(areaId));
        return response;
    }

    @Override
    public ResponseDTO<List<ProjectExtensionDicDTO>> allProjectExtensionDic(ProjectExtensionDicSearchDTO projectExtensionDicSearchDTO) {
        ResponseDTO<List<ProjectExtensionDicDTO>> response = new ResponseDTO<>();
        response.setData(projectExtensionDicManager.all(projectExtensionDicSearchDTO));
        return response;
    }

    @Override
    public ResponseDTO<List<ProjectExtensionDicDTO>> listByProjectId(Integer projectId) {
        ResponseDTO<List<ProjectExtensionDicDTO>> response = new ResponseDTO<>();
        response.setData(projectExtensionDicManager.listByProjectId(projectId));
        return response;
    }


    @Override
    public ResponseDTO<ProjectTagDTO> getProjectTag(Integer projectTagId) {
        ResponseDTO<ProjectTagDTO> response = new ResponseDTO<>();
        response.setData(projectTagManager.get(projectTagId));
        return response;
    }

    @Override
    public ResponseDTO<ProjectTagDTO> saveProjectTag(ProjectTagDTO dto) {
        ResponseDTO<ProjectTagDTO> response = new ResponseDTO<>();
        response.setData(projectTagManager.save(dto));
        return response;
    }

    @Override
    public ResponseDTO<Boolean> deleteProjectTag(Integer projectTagId) {
        ResponseDTO<Boolean> response = new ResponseDTO<>();
        response.setData(projectTagManager.delete(projectTagId));
        return response;
    }

    @Override
    public ResponseDTO<List<ProjectTagDTO>> allProjectTag(ProjectTagSearchDTO projectTagSearchDTO) {
        ResponseDTO<List<ProjectTagDTO>> response = new ResponseDTO<>();
        response.setData(projectTagManager.all(projectTagSearchDTO));
        return response;
    }

    @Override
    public ResponseDTO<ProjectAttachDTO> getProjectAttach(ProjectAttachSearchDTO projectAttachSearchDTO) {
        ResponseDTO<ProjectAttachDTO> response = new ResponseDTO<>();
        response.setData(projectAttachManager.get(projectAttachSearchDTO.getAttachId()));
        return response;
    }

    @Override
    public ResponseDTO<List<ProjectDTO>> listForOrg(ProjectSearchDTO var1) {
        ResponseDTO<List<ProjectDTO>> response = new ResponseDTO<>();
        response.setData(projectManager.listForOrg());
        return response;
    }

}
